<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="LAFDC.DataAccess" %>
<%@ Import Namespace="LAFDC.Logging" %>
<script language="c#" runat="server">
        public static readonly String source = "GetSectionByAD.aspx";
        private String section_array = String.Empty;

        protected void Page_Load(object sender, EventArgs e) {
            string ad = this.Request.QueryString.Get("AD");
            
            FDCLogger.LogEvent("Preparing to retrieve the section liste for AD " + ad, source, 4);

            ConnectionManager cn = new ConnectionManager();
            SqlConnection conn = cn.GetSqlConnection();
            SqlCommand tsql = conn.CreateCommand();
            
            tsql.CommandType = CommandType.Text;
            tsql.CommandText = @"exec getSectionByAD " + "'" + ad + "'";
            
            FDCLogger.LogEvent("Preparing to execute SQL statement: " + tsql.CommandText, source, 2);

            conn.Open();
            int c = 0;
            StringBuilder json_array = new StringBuilder();
            json_array.Append("[");

            using (SqlDataReader r = tsql.ExecuteReader())
            {
                while (r.Read())
                {
                    json_array.Append("'" + r.GetString(0) + "',");
                    c++; //Count of results to be reported
                }
            }
            json_array.Remove(json_array.Length-1, 1); //Remove trailing comma inserted during the last read
            json_array.Append("]");
            
            section_array = json_array.ToString();

            conn.Close();
            
            FDCLogger.LogEvent("Populated json array with:\n" + json_array.ToString(), source, 2);


        }
</script>
<%Response.Write(section_array);%>